package org.ala.tiktools.rocketmq.config;


import org.ala.tiktools.rocketmq.ConfigValueUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 配置信息
 *
 * @author ala
 * @date 2024-11-29 15:31
 */
public class ConfigInfo {


    public static ConfigInfo INSTANCE = null;


    /**
     *  rocket地址
     */
    protected String namesrvAddr;
    /**
     *  producer的扫描目录
     */
    protected String[] producerPackages;
    /**
     *  每个topic的默认队列数
     *  <p> 在 ConsumerClient 端可自定义
     */
    protected int queue = 8;
    /**
     *  雪花id机器码
     *  <p> 0 ~ 1023
     */
    protected long snowflow;
    /**
     *  配置信息
     */
    protected Environment env;
    /**
     *  要扫描的客户端包目录（不配默认全局扫描）
     */
    protected String scanClient;
    /**
     *  要扫描的客户端包目录（不配默认全局扫描）
     */
    protected String scanService;


    /**
     *  公共线程池配置
     */
    protected int threadPoolCoreSize = 5,
                  threadPoolMaxSize = 10,
                  threadPoolQueueSize = 1024;

    public static ConfigInfo build(Environment env) {
        ConfigInfo info = new ConfigInfo();
        info.env = env;

        if (!env.containsProperty("rocket.namesrvAddr")) {
            return null;
        }
        info.setNamesrvAddr(env.getProperty("rocket.namesrvAddr"));

        String pkgs = env.getProperty("rocket.producerPackages", String.class, "");
        if (StringUtils.isEmpty(pkgs)) {
            throw new RuntimeException("rocket.producerPackages 扫描目录不能为空（为空会扫描所有依赖的jar，慢不说还会带来乱七八糟的引用问题，比如Spring的JdbcTest）");
        }
        info.setProducerPackages(pkgs.split(";"));

        try {
            info.setQueue(Integer.parseInt(Objects.requireNonNull(env.getProperty("rocket.queue"))));
        } catch (Exception e) {
            info.setQueue(8);
        }

        try {
            info.setThreadPoolCoreSize(Integer.parseInt(Objects.requireNonNull(env.getProperty("rocket.threadPoolCoreSize"))));
            info.setThreadPoolMaxSize(Integer.parseInt(Objects.requireNonNull(env.getProperty("rocket.threadPoolMaxSize"))));
            info.setThreadPoolQueueSize(Integer.parseInt(Objects.requireNonNull(env.getProperty("rocket.threadPoolQueueSize"))));
        } catch (Exception e) {
            info.setThreadPoolCoreSize(5);
            info.setThreadPoolMaxSize(10);
            info.setThreadPoolQueueSize(1024);
        }

        try {
            info.setSnowflow(Long.parseLong(Objects.requireNonNull(env.getProperty("rocket.snowflowNode"))));
        } catch (Exception e) {
            info.setSnowflow(0l);
        }

        return info;
    }


    /**
     *  解析值中的配置项
     */
    public String replaceEnv(String value) {
        return ConfigValueUtils.replaceConfigItem(value, env);
    }


    public String getNamesrvAddr() {
        return namesrvAddr;
    }

    public void setNamesrvAddr(String namesrvAddr) {
        this.namesrvAddr = namesrvAddr;
    }

    public int getQueue() {
        return queue;
    }

    public void setQueue(int queue) {
        this.queue = queue;
    }

    public int getThreadPoolCoreSize() {
        return threadPoolCoreSize;
    }

    public void setThreadPoolCoreSize(int threadPoolCoreSize) {
        this.threadPoolCoreSize = threadPoolCoreSize;
    }

    public int getThreadPoolMaxSize() {
        return threadPoolMaxSize;
    }

    public void setThreadPoolMaxSize(int threadPoolMaxSize) {
        this.threadPoolMaxSize = threadPoolMaxSize;
    }

    public int getThreadPoolQueueSize() {
        return threadPoolQueueSize;
    }

    public void setThreadPoolQueueSize(int threadPoolQueueSize) {
        this.threadPoolQueueSize = threadPoolQueueSize;
    }

    public long getSnowflow() {
        return snowflow;
    }

    public void setSnowflow(long snowflow) {
        this.snowflow = snowflow;
    }

    public void setProducerPackages(String[] producerPackages) { this.producerPackages = producerPackages; }

    public String[] getProducerPackages() {  return producerPackages; }
}
